package demo530;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class EmployeeDao {

    private static final String URL = "jdbc:mysql://localhost:3306/employees";
    private static final String USER = "root";
    private static final String PASSWORD = "123456";

    // 添加员工信息到数据库
    public void addEmployee(Employee employee) {
        String sql = "INSERT INTO employees (id, name, password, age) VALUES (?, ?, ?, ?)";

        try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, employee.getId());
            pstmt.setString(2, employee.getName());
            pstmt.setString(3, employee.getPassword());
            pstmt.setInt(4, employee.getAge());

            int rowsAffected = pstmt.executeUpdate();
            if (rowsAffected > 0) {
                System.out.println("员工信息添加成功！");
            } else {
                System.out.println("员工信息添加失败！");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

        }




